package com.steerinfo.dil.util;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.xslf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 *
 * @author ruand http://blog.csdn.net/emoven/article/details/52683215
 */
public class POIPptToHtml {

	private final static String PPT = "ppt";
	private final static String PPTX = "pptx";

	public static String pptToHtml(InputStream is, String type) {
		String htmlStr = "预览失败";
		try {
				if (PPT.equals(type)) {
					 htmlStr = toImage2003(is);
				} else if (PPTX.equals(type)) {
					 htmlStr = toImage2007(is);
				} else {
					htmlStr = "the file is not a ppt";
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return htmlStr;
	}

	public static String toImage2007(InputStream is) throws Exception {
		String htmlStr = "预览失败";
		XMLSlideShow ppt = new XMLSlideShow(is);
		is.close();
		Dimension pgsize = ppt.getPageSize();
		System.out.println(pgsize.width + "--" + pgsize.height);

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ppt.getSlides().size(); i++) {
			try {
				// 防止中文乱码
				for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) {
					if (shape instanceof XSLFTextShape) {
						XSLFTextShape tsh = (XSLFTextShape) shape;
						for (XSLFTextParagraph p : tsh) {
							for (XSLFTextRun r : p) {
								r.setFontFamily("宋体");
							}
						}
					}
				}
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
				// render
				ppt.getSlides().get(i).draw(graphics);
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				ImageIO.write(img, "png", stream);
				String imgStr = Base64.getEncoder().encodeToString(stream.toByteArray());
				// save the output
				sb.append("<br>");
				sb.append("<img src=" + "\"data:image/png;base64," + imgStr + "\"" + "/>");
				stream.close();
			} catch (Exception e) {
				System.out.println("第" + i + "张ppt转换出错");
			}
		}
		System.out.println("success");
		htmlStr = sb.toString();

		return htmlStr;
	}

	public static String toImage2003(InputStream is) {
		String htmlStr = "预览失败";
		try {
			HSLFSlideShow ppt = new HSLFSlideShow(is);
			Dimension pgsize = ppt.getPageSize();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < ppt.getSlides().size(); i++) {
				// 防止中文乱码
				for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {
					if (shape instanceof HSLFTextShape) {
						HSLFTextShape tsh = (HSLFTextShape) shape;
						for (HSLFTextParagraph p : tsh) {
							for (HSLFTextRun r : p) {
								r.setFontFamily("宋体");
							}
						}
					}
				}
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
				// render
				ppt.getSlides().get(i).draw(graphics);
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				//String imageDir = targetDir + "/" + pptFileName + "/";
				//FileUtils.createDir(imageDir);// create image dir
				//String imagePath = imageDir + pptFileName + "-" + (i + 1) + ".png";
				ImageIO.write(img, "png", stream);
				String imgStr = Base64.getEncoder().encodeToString(stream.toByteArray());
				sb.append("<br>");
				sb.append("<img src=" + "\"data:image/png;base64," + imgStr + "\"" + "/>");
				stream.close();

			}
			System.out.println("success");
			htmlStr = sb.toString();
		} catch (Exception e) {

		}
		return htmlStr;
	}

	/***
	 * 功能 :调整图片大小
	 *
	 * @param srcImgPath
	 *            原图片路径
	 * @param distImgPath
	 *            转换大小后图片路径
	 * @param width
	 *            转换后图片宽度
	 * @param height
	 *            转换后图片高度
	 */
	public static void resizeImage(String srcImgPath, String distImgPath, int width, int height) throws IOException {

		File srcFile = new File(srcImgPath);
		Image srcImg = ImageIO.read(srcFile);
		BufferedImage buffImg = null;
		buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

		ImageIO.write(buffImg, "JPEG", new File(distImgPath));

	}
}
